<template>
    <div class="register">
        <div class="second_center">
            <div style="margin: 10px"></div>
            <el-form label-position="right" label-width="5px" :model="loginForm" style="max-width: 240px">
                <el-form-item>
                    <el-input v-model="loginForm.uname" maxlength="10" show-word-limit placeholder="  输入用户名" />
                </el-form-item>
                <el-form-item>
                    <el-input v-model="loginForm.password" type="password" placeholder="  输入密码" show-password />
                </el-form-item>
                <el-form-item>
                    <el-input v-model="loginForm.mail" placeholder="  输入邮件" />
                </el-form-item>
                <el-form-item>
                    <el-select v-model="loginForm.sex" placeholder="请选择性别" style="width:240px;">
                        <el-option label="男" value="男"></el-option>
                        <el-option label="女" value="女"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="loginForm.phone" placeholder="  输入手机号" />
                </el-form-item>
                <el-form-item>
                    <el-button style="width:240px;font-size: large; " round @click="handleRegister">注册</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button style="width:240px;font-size: large; " round @click="toIndex">返回</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script setup lang="ts">
import { IUser } from '../api/x-type'
import { register } from '../api/x-user'
import { login, setToken } from '../api/user';
import * as message from '../utils/message-utils'

const router = useRouter()
const loginForm = reactive<IUser>({
    uname: '',
    password: '',
    mail: '',
    sex: '',
    phone: ''
})

const toIndex = () => {
    router.push('/')
}

const handleRegister = () => {
    register(loginForm).then(data => {
        if (data.code == 200) {
            login(loginForm.uname, loginForm.password).then(data => {
                //@ts-ignore
                message.success(data.msg)
                //@ts-ignore
                setToken(data.data)
                router.push('/home')
            })
        } else {
            message.error(data.msg)
        }
    })
}

</script>


<style lang="scss" scoped>
.register {
    display: flex;
    height: 100vh;
    align-items: center;
    justify-content: center;
    background-image: url('@/assets/images/regist_bg.jpg');
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: center center;
    background-size: cover;
}

.second_center {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0px;
    width: auto;
    height: auto;
}
</style>